//my.js
//获取应用实例
var app = getApp();
var util = require("../../utils/util");
var myUtil = require("../../utils/myUtil");
Page({
  data: {
    headerIndex: 1, //1-订单;2-收藏;3-资料
    favoriteIndex: 1, // 1-成衣收藏,2-面料收藏
    profileFormHide: true, // 是否隐藏资料提交表单
    startX: 0, //开始x坐标
    startY: 0, //开始y坐标
    orderList: [], // 订单列表
    favoriteList: [], // 收藏的产品 包括衣服 面料
    favoriteClothNum: 0,
    favoriteFabricNum: 0,
    userInfo: {}, // 用户资料

    pageNo : 0,  // 当前分页
    pageSize : 4, // 单页数量
    totalPage : 0, // 总页数
    totalNum : 0, // 总数量

    orderProduct: {}, // 商品信息
    orderSku: {}, // 商品SKU信息

    favoriteProduct: {} // 收藏商品信息
  },

  onLoad:function(options){
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight:res.windowHeight,
          windowHeight:res.windowHeight
        });
      }
    });
  },
  onShow: function(){

    // 获取订单列表
    myUtil.getMyOrderList(this, 1);
    // 获取收藏
    myUtil.getMyFavoriteList(this);

    myUtil.getMyInfo(function(data){
      that.setData({userInfo: data});
    });
  },
  bindDownload: function(){
    // 该方法绑定页面滑动到底部事件
    console.log("download");
    this.setData({
      loadHide:false
    });
    myUtil.getMyOrderList(this, 0);
  },
  scroll: function(e){
    // 滚动事件
    // this.setData({
    //   scrollTop: e.detail.scrollTop
    // });
  },
  touchstart: function(e){
    //开始滑动
    this.data.favoriteList.forEach(function(v, i){
      if(v.isTouchMove){
        v.isTouchMove = false;
      }
    });
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      favoriteList: this.data.favoriteList
    });
  },
  touchmove: function(e){
    var that = this,
    index = e.currentTarget.dataset.index, // 当前索引
    startX = that.data.startX,
    startY = that.data.startY,
    touchMoveX = e.changedTouches[0].clientX,
    touchMoveY = e.changedTouches[0].clientY,
    angle = that.angle({X:startX, Y:startY}, {X:touchMoveX, Y:touchMoveY});

      // 成衣
      that.data.favoriteList.forEach(function(v, i){
        v.isTouchMove = false;
        if(Math.abs(angle) > 30) return;
        if(i == index){
          if(touchMoveX > startX){
            // 右滑
            v.isTouchMove = false;
          }else{
            // 左滑
            v.isTouchMove = true;
          }
        }
      });
    
    
    that.setData({
      favoriteList : that.data.favoriteList
    });
  },
  angle: function(start, end){
    var _X = end.X - start.X,
    _Y = end.Y - start.Y;

    return 360 * Math.atan(_Y/_X) / (2 * Math.PI);
  },
  del: function(e){
    var that = this;
    var item = this.data.favoriteList[e.currentTarget.dataset.index];

    myUtil.delMyFavorite(item.id, function(){
      that.data.favoriteList.splice(e.currentTarget.dataset.index, 1);
      if(item.response.parent_id){
        // 成衣
        that.setData({
          favoriteList: that.data.favoriteList,
          favoriteClothNum: that.data.favoriteClothNum--
        });
      }else{
        // 面料
        that.setData({
          favoriteList: that.data.favoriteList,
          favoriteFabricNum: that.data.favoriteFabricNum--
        });
      }
    });
  },

  onHeadTab: function(e){
    var that = this;
    this.setData({
      headerIndex: e.currentTarget.dataset.typeid
    });
    if(e.currentTarget.dataset.typeid == 2){
      // 收藏
      myUtil.getMyFavoriteList(this);
    }else if(e.currentTarget.dataset.typeid == 3){
      // 信息
      myUtil.getMyInfo(function(data){
        that.setData({userInfo: data});
      });
    }
  },
  onFavoriteHeadTab: function(e){
    this.setData({
      favoriteIndex: e.currentTarget.dataset.typeid
    });
  },
  editProfile: function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '编辑资料则需要重新认证，是否确认编辑',
      cancelColor: '#a5a5a9',
      confirmText: '确认',
      confirmColor: '#111111',
      success: function(res){
        if(res.confirm){
            that.setData({profileFormHide: false});
        }
      },
      fail: function(){}
    });
  },
  formSubmit: function(e) {
    var that = this;
    var valueObj = e.detail.value;
    if(util.trim(valueObj.userName) == "" || util.trim(valueObj.userCompany) == "" || util.trim(valueObj.userTel) == "" || util.trim(valueObj.userAddress) == ""){
      return false;
    }

    if(!util.checkPhoneNum(valueObj.userTel)){
      wx.showModal({
        title: '提示',
        content: '电话号码格式错误',
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#111111',
        success: function(res){},
        fail: function(){}
      });
      return false;
    }

    var dataObj = {
        "name": valueObj.userName,
        "company": valueObj.userCompany,
        "phone": valueObj.userTel,
        "address" : valueObj.userAddress,
    };
    
    if(this.data.userInfo.id){
      myUtil.updateMyInfo(this.data.userInfo.id, dataObj, function(res){
          that.setData({userInfo: res, profileFormHide: true});
      });
    }else{
      myUtil.addMyInfo(dataObj, function(res){
        that.setData({userInfo: res, profileFormHide: true});
      });
    }
  },
  formReset: function() {
    this.setData({profileFormHide: true});
  },
  gotoHome: function(e){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  gotoDetail: function(e){
    var productId = e.currentTarget.dataset.productid;
    var productName = e.currentTarget.dataset.productname;
    var type = e.currentTarget.dataset.type;
    var url = type == 'cloth' ? '/pages/clothDetail/clothDetail' : '/pages/fabricDetail/fabricDetail';
    wx.navigateTo({
        url: url + '?productId='+productId+'&productName='+productName
    })
  }
})

